import { DataSource } from "typeorm";
import { getAllEntity } from "../modules/modelSetup";

const env = process.env;

const appDataSource = new DataSource({
  type: "mysql",
  host: env.DB_HOST,
  username: env.Y_DB_USERNAME,
  password: env.Y_DB_PASSWORD,
  database: env.Y_DB_DATABASE,
  timezone: "+08:00",
  dateStrings: true,
});

(async () => {
  try {
    
 
  // 加载所有实体
  const entities = await getAllEntity();
  appDataSource.setOptions({
    entities,
  });
  // 初始化数据库
  await appDataSource.initialize()

  // 同步模型
  await appDataSource.synchronize()
  console.log('数据库同步成功');

} catch (error) {
  console.log(error);
}
})();

export default appDataSource;